package com.eightywork.life.activity;

import java.util.List;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageButton;
import android.widget.TextView;

import cn.jpush.android.api.JPushInterface;

import com.eightywork.life.R;
import com.eightywork.life.adapter.PmListAdapter;
import com.eightywork.life.model.Pm;
import com.eightywork.life.service.SpaceService;
import com.eightywork.life.util.MyHttpException;
import com.eightywork.life.widget.FooterView;
import com.eightywork.life.widget.MyListView;
import com.eightywork.life.widget.MyListView.OnRefreshListener;
import com.umeng.analytics.MobclickAgent;

public class PmListActivity extends BaseActivity implements OnClickListener {
	private ImageButton backButton;
	private ImageButton postButton;
	private PmListAdapter adapter;
	private TextView headerTitile,footerText; 
	private MyListView listView;
	private boolean isLoading, isRefresh = false;
	private int page = 1;
	private FooterView footerView;
	private int flag = 0; //第一次加载
	private AsyncTask<?, ?, ?> task;
//	private ViewGroup footView, nodataView;
	public static List<Pm> list;
	private boolean isFromReceive;
//	private long count;
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_pm_list);	
		initView();
		initData();
		getPmList();
	}
	@Override
	protected void onDestroy() {
		if (task != null) {
			task.cancel(true);
		}
		super.onDestroy();
	}
	
	@Override
	public void onResume() {		
		super.onResume();
		JPushInterface.onResume(this);
	    MobclickAgent.onResume(this);
	}
	
	@Override
	public void onPause() {
	    super.onPause();
		JPushInterface.onPause(this);
	    MobclickAgent.onPause(this);
	}

	
	private void initView() {
		postButton = (ImageButton)findViewById(R.id.button_right);
		backButton = (ImageButton)findViewById(R.id.button_back);
		headerTitile = (TextView)findViewById(R.id.header_title);
		listView = (MyListView)findViewById(R.id.listView_pm2);
		footerView = new FooterView(this);
		footerView.hideAll();
		postButton.setOnClickListener(this);
		backButton.setOnClickListener(this);
		
	}
	
	private void initData() {
		initHeader("私信列表");
		adapter = new PmListAdapter(context, list);
		listView.setAdapter(adapter);
		listView.setonRefreshListener(new OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub
			//	isLoading = false;
		//		page = (flag == 0)? 1: 0;
				isLoading = false;
				isRefresh = true;
				task = new PmListTask().execute();
			}
		});
//		listView.setOnScrollStopAndEndListener(new OnScrollStopAndEndListener() {
//			
//			@Override
//			public void onEndAndStopScroll() {
//				// TODO Auto-generated method stub
//				if (!isLoading) {
//					getPmList();
//				}
//			}
//		});
//		listView.goLoading();
		
	}
	
	private void getPmList() {
		task = new PmListTask().execute();
		
	}
	
	class PmListTask extends AsyncTask<Void, Void, String> {
		List<Pm> plist;
		@Override
		protected void onPreExecute() {
			if(flag == 0){
				showProgressDialog("正在加载中...");
			}else {
				page = page + 1;
			}
			isLoading = true;
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			try {
				plist = SpaceService.pm(page + "", "");
				
			} catch (MyHttpException e) {
				// TODO Auto-generated catch block
				showMessage(e.getErrorMessage());
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			isLoading = false;
			if (flag == 0) {
				hideProgressDialog(); 
			}
			if (isRefresh) {	
				listView.onRefreshComplete();
			} 
			if (plist != null && plist.size() > 0) {
				if (flag == 0) {
					adapter = new PmListAdapter(context, plist);
					listView.addFooterView(footerView.getFooter());
					listView.setAdapter(adapter);
			    	listView.setOnScrollListener(new ScrollListener());
					list = plist;
					
				} else if (isRefresh) {
					isRefresh = false;
					adapter = new PmListAdapter(context, plist);				
					listView.setAdapter(adapter);
					listView.setOnScrollListener(new ScrollListener());
					list = plist;
					listView.onRefreshComplete();
				} else {
					list.addAll(plist);
					adapter.notifyDataSetChanged();
				}
				if (plist.size() < 5) {
					footerView.showNoData();
				}
			} else {
				if (plist != null) {
					if (flag == 0) {
						listView.addFooterView(footerView.getFooter());
					}
					footerView.showNoData();
	//				listView.addFooterView(nodataView);	
		//			footerText.setText("没有更多内容");				
				} else {
					footerView.hideAll();
					showMessage("网络链接失败，请稍后重试！");
				}
				
			}
			flag = 1;
			
		}
		
	}
	
	 private class ScrollListener implements OnScrollListener
		{
			private int listViewLastItem = 0;
			private boolean isOver = false;
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
			{
				if (adapter.getCount() % page > 0 || adapter.getCount() == 0)
				{
					footerView.hideAll();
					isOver = true;
				}
				listViewLastItem = firstVisibleItem + visibleItemCount - 1; 
			}

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState)
			{    
				if (!isOver && !isLoading && listViewLastItem >= adapter.getCount()-5 
						&& scrollState == OnScrollListener.SCROLL_STATE_IDLE)
				{
					footerView.showLoading();
					task = new PmListTask().execute();
				}

			}
		}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == backButton) {
			finish();
		} else if (v == postButton) {
			Intent intent = new Intent(context, SelectNeighbourActivity.class);
			startActivity(intent);
		}
	}

}
